package com.bm.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.bm.dao.BaseDao;
import com.bm.dao.ImgDao;
import com.bm.entity.Img;
import com.bm.entity.onenNew;
import com.bm.entity.product;
import com.bm.entity.project;

public class ImgDaoImpl extends BaseDao implements ImgDao {

	//查找
	public List<Img> qureyAllImg(String xlName) {
		// TODO Auto-generated method stub
		Connection conn = BaseDao.getConn();
		PreparedStatement s = null;
		ResultSet re = null;
		List<Img> imgList = new ArrayList<Img>();
		try {
			String sql = "SELECT * FROM "+xlName;
			s = conn.prepareStatement(sql);
			re = s.executeQuery();
			while(re.next()) {
				Img img = new Img();
				img.setId(re.getInt("bmId"));
				img.setImgurl(re.getString("imageName"));
				img.setTitle(re.getString("imageTitle"));
				img.setType(re.getString("imageType"));
				imgList.add(img);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			BaseDao.closeAll(conn, s, re);
		}
		return imgList;
	}
	//添加
	public int insert(Img img) {
		// TODO Auto-generated method stub
		String sql = "INSERT INTO "+img.getBsName()+"(imageName,imageType,imageTitle) VALUE  (?,?,?)";
		Object[]  objs = {img.getImgurl(),img.getType(),img.getTitle()};
 		return BaseDao.executeUpdate(sql, objs);
	}
	//删除
	public int deleteImg(int id ,Img img) {
		// TODO Auto-generated method stub
		String sql = "DELETE FROM "+img.getBsName()+" WHERE bmId ="+id;
 		return BaseDao.executeUpdate(sql);
	}
	
	
	
	//前
	
	@Override
	public List<product> seletPage(String page) {
		// TODO Auto-generated method stub
		String sql = null;
		if(page.equals("6-1")) {
			sql = "SELECT * FROM blackone";
		}else if(page.equals("9-1")) {
			sql = "SELECT * FROM white";
		}else if(page.equals("7-1")) {
			sql = "SELECT * FROM blacktwo";
		}else if(page.equals("6-2")) {
			sql = "SELECT * FROM color";
		}else if(page.equals("6-3")) {
			sql = "SELECT * FROM greeblack";
		}else if(page.equals("6-4")) {
			sql = "SELECT * FROM sand";
		}else if(page.equals("6-5")) {
			sql = "SELECT * FROM multicolored";
		}else if(page.equals("6-6")) {
			sql = "SELECT * FROM mediumgray";
		}else if(page.equals("10-2")) {
			sql = "SELECT * FROM grey";
		}else if(page.equals("6-7")) {
			sql = "SELECT * FROM lightgray";
		}else if(page.equals("6-8")) {
			sql = "SELECT * FROM seashell";
		}else if(page.equals("6-9")) {
			sql = "SELECT * FROM glass";
		}else if(page.equals("6-10")) {
			sql = "SELECT * FROM Dandong";
		}else if(page.equals("6-11")) {
			sql = "SELECT * FROM pink";
		}else if(page.equals("6-12")) {
			sql = "SELECT * FROM red";
		}else if(page.equals("6-13")) {
			sql = "SELECT * FROM blue";
		}else if(page.equals("6-14")) {
			sql = "SELECT * FROM green";
		}else if(page.equals("6-15")) {
			sql = "SELECT * FROM yellow";
		}
		List<product> list = new ArrayList<product>();
		
		ResultSet rs=null;
		try {
			rs = super.queryResultSet(sql);		
			
			while(rs.next()) {
				product product = new product();
				product.setProductImg(rs.getString("imageName"));
				product.setProductType(rs.getString("imageType"));
				product.setProductTitle(rs.getString("imageTitle"));
				list.add(product);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			super.closeAll(super.conn, super.ps,rs);
		}
		return list;

	}

	@Override
	public boolean uWord(String uName, String uPhone, String uWord) {
		// TODO Auto-generated method stub
		String sql = "INSERT INTO userWord(uName,uPhone,uWord) VALUES ('"+uName+"','"+uPhone+"','"+uWord+"');";
		System.out.println(sql);
		int num = super.executeUpdate(sql);
		if(num==1) {
			return true;
		}
		return false;
	}
	@Override
	public List<project> selectProject() {
		// TODO Auto-generated method stub
		String sql = "SELECT * FROM projectShow";
		List<project> list = new ArrayList<project>();
		ResultSet rs=null;
		try {
			rs = super.queryResultSet(sql);		
			
			while(rs.next()) {
				project project = new project();
				project.setProjectImage(rs.getString("projectImage"));
				project.setProjectName(rs.getString("projectName"));
				list.add(project);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			super.closeAll(super.conn, super.ps,rs);
		}
		return list;
	}
	@Override
	public List<onenNew> selectNew() {
		// TODO Auto-generated method stub
		String sql = "SELECT * FROM tnews";
		List<onenNew> list = new ArrayList<onenNew>();
		ResultSet rs=null;
		try {
			rs = super.queryResultSet(sql);		
			
			while(rs.next()) {
				onenNew onenNew = new onenNew();
				onenNew.setNewImage(rs.getString("newsImage"));
				onenNew.setNewTitle(rs.getString("newsName"));
				onenNew.setNewWord(rs.getString("newsTitle"));
				onenNew.setNewsContent(rs.getString("newsContent"));
				list.add(onenNew);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			super.closeAll(super.conn, super.ps,rs);
		}
		return list;
	}
	@Override
	public List<onenNew> selectTwoNew(String id) {
		// TODO Auto-generated method stub
			String sql = "SELECT * FROM tnews WHERE newsName='"+id+"';";
			System.out.println(sql);
			List<onenNew> list = new ArrayList<onenNew>();
			ResultSet rs=null;
			try {
				rs = super.queryResultSet(sql);		
				
				while(rs.next()) {
					onenNew onenNew = new onenNew();
					onenNew.setNewImage(rs.getString("newsImage"));
					onenNew.setNewTitle(rs.getString("newsName"));
					onenNew.setNewWord(rs.getString("newsTitle"));
					onenNew.setNewsContent(rs.getString("newsContent"));
					list.add(onenNew);
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally {
				super.closeAll(super.conn, super.ps,rs);
			}
			return list;
	}
	
	
	
	
	
}
